-- Tags: long

DROP TABLE IF EXISTS left;
DROP TABLE IF EXISTS right;

SET join_algorithm = 'partial_merge';

{% for block_size in [10, 11, 128, 129, 65505, 65506, 70000] -%}
{% for join_block_size in range(block_size - 2, block_size + 2) -%}

DROP TABLE IF EXISTS left;
DROP TABLE IF EXISTS right;

CREATE TABLE left ( key UInt32, value String ) ENGINE = Memory;
CREATE TABLE right (  key UInt32, value String ) ENGINE = Memory;

INSERT INTO left SELECT number, toString(number) FROM numbers({{ block_size * 2 + 1 }});
INSERT INTO right SELECT number, toString(number) FROM numbers({{ block_size * 2 + 5 }});

SET max_joined_block_size_rows = {{ join_block_size }};
SET max_block_size = {{ block_size }};

SELECT key, count(1) AS cnt
FROM (
    SELECT *
    FROM ( SELECT key FROM left AS s ) AS data
    ALL LEFT JOIN ( SELECT key FROM right GROUP BY key ) AS promo ON promo.key = data.key
) GROUP BY key HAVING count(1) > 1
;

SELECT count() == (SELECT count() from left) AND min(key == promo.key) == 1
FROM ( SELECT key FROM left AS s ) AS data
ALL LEFT JOIN ( SELECT key FROM right GROUP BY key ) AS promo ON promo.key = data.key
;

{% endfor -%}
{% endfor -%}

DROP TABLE IF EXISTS left;
DROP TABLE IF EXISTS right;
